<template>
	<view>
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="content">调岗申请</block>
		</cu-custom>
		<form>
			<view class="cu-form-group">
				<view class="title">用户</view>
				<my-select range-key="text" value-key="value" dict-code="sys_user,realname,id"
					v-model="myFormData.userId" />
			</view>			
			
			<view class="cu-form-group">
				<view class="title">工号</view>
				<input placeholder="请输入工号" name="input" v-model="myFormData.workNo"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">公司</view>
				<my-select range-key="text" value-key="value" dict-code="crm_company,name,id"
					v-model="myFormData.company" />
			</view>
			<view class="cu-form-group">
				<view class="title">部门</view>
				<text>{{myFormData.departTitle}}</text>
				<view class="cu-capsule radius">
					<view class="cu-tag line-blue" @tap="ChooseDepartIds">
						选择部门
					</view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">职务</view>
				<my-select range-key="text" value-key="value" dict-code="sys_position,name,code"
					v-model="myFormData.post" />
			</view>
			<view class="cu-form-group">
				<view class="title">职务等级</view>
				<my-select range-key="text" value-key="value" dict-code="post_level"
					v-model="myFormData.postLevel" />
			</view>
			<view class="cu-form-group">
				<view class="title">职务归属</view>
				<my-select range-key="text" value-key="value" dict-code="post_belong"
					v-model="myFormData.postBelong" />
			</view>
			
			<view class="cu-form-group">
				<view class="title">调岗类型</view>
				<my-select range-key="text" value-key="value" dict-code="job_transfer_type"
					v-model="myFormData.jobTransferType" />
			</view>
			<view class="cu-form-group">
				<view class="title">是否使用新工号</view>
				<my-select range-key="text" value-key="value" dict-code="yn"
					v-model="myFormData.newWorkNoFlag" />
			</view>
			<view class="cu-form-group">
				<view class="title">新工号</view>
				<input placeholder="请输入新工号" name="input" v-model="myFormData.newWorkNo"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">新公司</view>
				<my-select range-key="text" value-key="value" dict-code="crm_company,name,id"
					v-model="myFormData.newCompany" />
			</view>
			<view class="cu-form-group">
				<view class="title">新部门</view>
				<text>{{myFormData.newDepartTitle}}</text>
				<view class="cu-capsule radius">
					<view class="cu-tag line-blue" @tap="ChooseDepartIds('newDepart')">
						选择新部门
					</view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">新职务</view>
				<my-select range-key="text" value-key="value" dict-code="sys_position,name,code"
					v-model="myFormData.newPost" />
			</view>
			<view class="cu-form-group">
				<view class="title">新职务等级</view>
				<my-select range-key="text" value-key="value" dict-code="post_level"
					v-model="myFormData.newPostLevel" />
			</view>
			<view class="cu-form-group">
				<view class="title">新职务归属</view>
				<my-select range-key="text" value-key="value" dict-code="post_belong"
					v-model="myFormData.newPostBelong" />
			</view>
			<view class="cu-form-group">
				<view class="title">调岗原因</view>
				<input placeholder="请输入调岗原因" name="input" v-model="myFormData.reason"></input>
			</view>

			
			



			<!-- 提交 -->
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-blue lg" @click="onSubmit">提交</button>
			</view>
		</form>

	</view>
</template>

<script>
	import myDate from '@/components/my-componets/my-date.vue'
	import mySelect from '@/components/my-componets/my-select.vue'
	export default {
		components: {
			myDate,
			mySelect
		},
		data() {
			return {
				regularDate: '2000-01-01',
				listTouchStart: 0,
				listTouchDirection: null,
				modalName: null,
				index: -1,
				switchC: true,
				imgList: [],
				imgListIdCardCopy: [],
				uploadUrl: "/sys/common/upload",
				departFrom: '',
				myFormData: {
					userId: '',
					workNo:'',
					company:'',
					departIds:'',
					post:'',
					postLevel:'',
					postBelong:'',
					jobTransferType: '',
					newWorkNoFlag: '',
					newWorkNo: '',
					newCompany: '',
					newDepartIds: '',
					newPost: '',
					newPostLevel: '',
					newPostBelong: '',
					reason: '',
				},
			};
		},
		onLoad: function(option) {
			console.log("this.$Route.query", this.$Route.query);
			let query = this.$Route.query
			if (query) {
				this.myFormData = query;
				if (this.myFormData.sex == '女') {
					this.switchC = false
				} else if (this.myFormData.sex == '男') {
					this.switchC = true
				}
				if (this.myFormData.avatar) {
					this.imgList = [this.myFormData.avatar]
				}
				if (this.myFormData.idCardCopy) {
					this.imgListIdCardCopy = [this.myFormData.idCardCopy]
				}
				if (this.myFormData.identity == '普通成员') {
					this.myFormData.identity = 1
				} else if (this.myFormData.identity == '上级') {
					this.myFormData.identity = 2
				}
				if (this.myFormData.status == '正常') {
					this.myFormData.status = 1
				} else if (this.myFormData.status == '冻结') {
					this.myFormData.status = 2
				}
				this.Avatar = this.myFormData.avatar
				this.AvatarIdCardCopy = this.myFormData.idCardCopy

				Object.keys(this.myFormData).map(key => {
					if (this.myFormData[key] == '无') {
						this.myFormData[key] = ''
					}
				})
				console.log("this.myFormData==", this.myFormData)
			}

			uni.$on('departCallBack', (data) => {
				if (this.departFrom == 'newDepart') {
					this.$set(this.myFormData, 'newDepartIds', data?.idArray?.join(','))
					this.$set(this.myFormData, 'newDepartTitle', data?.titleArray?.join('>'))
				} else {
					this.$set(this.myFormData, 'departIds', data?.idArray?.join(','))
					this.$set(this.myFormData, 'departTitle', data?.titleArray?.join('>'))
				}
			})
		},
		methods: {
			onSubmit() {
				let myForm = this.myFormData
				let checkPhone = new RegExp(/^[1]([3-9])[0-9]{9}$/);
				let checkEmail = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
				console.log("myForm", myForm)
				//  if(!myForm.phone || myForm.phone.length==0){
				// this.$tip.alert('请输入手机号');
				// return false
				//  }
				//  if(!checkPhone.test(myForm.phone)){
				// this.$tip.alert('请输入正确的手机号');
				// return false
				//  }
				//  if(!checkEmail.test(myForm.email)){
				// this.$tip.alert('请输入正确的邮箱地址');
				// return false
				//  }
				this.myFormData.id = this.$store.getters.userid
				if (this.switchC) {
					this.myFormData.sex = 1
				} else {
					this.myFormData.sex = 2
				}
				console.log('myform', this.myFormData)
				this.$tip.loading();
				this.$http.post('/crm_job_transfer_application/crmJobTransferApplication/add', this.myFormData)
					.then(res => {
						console.log(res)
						this.$tip.loaded();
						if (res.data.success) {
							this.$tip.toast('提交成功')
							// this.$Router.replace({name:'userdetail'})
							/* uni.navigateTo({
								url: '/pages/user/userdetail'
							}) */
						}
					}).catch(() => {
						this.$tip.loaded();
						this.$tip.error('提交失败')
					});
			},
			DateChangeRegularDate(e) {
				this.regularDate = e.detail.value
				this.myFormData.regularDate = e.detail.value
			},

			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			deleteData(index, type) {
				let temp = this.myFormData[type]
				console.log(index, type, temp)
				temp.splice(index, 1);
				this.$set(this.myFormData, type, temp)
			},
			editData(index, type) {
				this.modalName = type
				this[type] = this.myFormData[type][index]
			},
			onSubmitData(type,
				defaultValue = {}) {
				console.log(type, this.myFormData[type], this[type])
				let temp = this.myFormData[type]
				if (temp == undefined) {
					temp = []
				}
				temp.push(this[type])
				this.$set(this.myFormData, type, temp)
				this[type] =
					defaultValue
				this.modalName = null
			},

			// ListTouch触摸开始
			ListTouchStart(e) {
				this.listTouchStart = e.touches[0].pageX
			},
			// ListTouch计算方向
			ListTouchMove(e) {
				this.listTouchDirection = e.touches[0].pageX - this.listTouchStart > 0 ? 'right' : 'left'
			},
			// ListTouch计算滚动
			ListTouchEnd(e) {
				if (this.listTouchDirection == 'left') {
					this.modalName = e.currentTarget.dataset.target
				} else {
					this.modalName = null
				}
				this.listTouchDirection = null
			},
			hideModal(e) {
				this.modalName = null
			},
			SwitchC(e) {
				this.switchC = e.detail.value
			},
			ChooseDepartIds(from = '') {
				this.departFrom = from;
				console.log("==========")
				uni.navigateTo({
					url: '/pages/oa/common/depart',
				});
			},
			ChooseImage() {
				var that = this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						that.$http.upload(that.$config.apiUrl + that.uploadUrl, {
								filePath: res.tempFilePaths[0],
								name: 'file'
							})
							.then(res => {
								that.myFormData.avatar = res.data.message;
							})
							.catch(err => {
								that.$tip.error(err.data.message)
							});
						this.imgList = res.tempFilePaths
					}
				});
			},
			ChooseImageIdCardCopy() {
				var that = this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						that.$http.upload(that.$config.apiUrl + that.uploadUrl, {
								filePath: res.tempFilePaths[0],
								name: 'file'
							})
							.then(res => {
								that.myFormData.idCardCopy = res.data.message;
							})
							.catch(err => {
								that.$tip.error(err.data.message)
							});
						this.imgListIdCardCopy = res.tempFilePaths
						console.log(this.imgListIdCardCopy)
					}
				});
			},
			ChooseImageHonorsCopy() {
				var that = this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						that.$http.upload(that.$config.apiUrl + that.uploadUrl, {
								filePath: res.tempFilePaths[0],
								name: 'file'
							})
							.then(res => {
								that.honors.images.push(res.data.message)
							})
							.catch(err => {
								that.$tip.error(err.data.message)
							});
						this.honors.localImages = res.tempFilePaths
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '召唤师',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			}
		},
		watch: {
			"myFormData.familyMembers": function(newName, oldName) {
				console.log('familyMembers==', newName, oldName)
			}
		}
	}
</script>

<style scoped>
	.cu-modal {
		z-index: 99;
	}

	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	.fontUnify {
		font-size: 14px;
	}

	.page {
		height: 100Vh;
		width: 100vw;
	}

	.page.show {
		overflow: hidden;
	}

	.switch-sex::after {
		content: "\e716";
	}

	.switch-sex::before {
		content: "\e7a9";
	}

	.switch-music::after {
		content: "\e66a";
	}

	.switch-music::before {
		content: "\e6db";
	}

	.cu-form-group+.cu-form-group {
		border: none;
	}

	.cu-list {
		background-color: #ffffff;

	}
</style>